package com.stars.home.activity

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.lifecycleScope
import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.baidu.mapapi.model.LatLng
import com.baidu.mapapi.utils.DistanceUtil
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.home.R
import com.stars.home.base.MBActivity
import com.stars.home.databinding.ActivityMainBinding
import com.stars.home.fragment.HomeFragment
import com.stars.home.fragment.HomeMeFragemnt
import com.stars.home.fragment.HomeOrderFragment
import com.stars.home.inte.ChoiceLocation
import com.stars.home.update.showUpgradeDialog
import com.stars.home.utils.CurrentLocationUtils
import com.stars.home.vmodel.MainVModel
import com.trailer.base.base.BApp
import com.trailer.base.utils.KeySimulateUtils
import java.util.*

/**
 *
 *on 2020/12/4
 */
class MainActivity : MBActivity<MainVModel, ActivityMainBinding>() {

    var fragments = emptyArray<Fragment>()

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        initData()


        vm.getAppConfig()

        vm.bean.observe(this){

            var code= BApp.instance()?.config()?.getVersionCode()
            if(code!!<it.version) {
                showUpgradeDialog(it)
            }
        }


        //检查更新
//        UpdateUtils.checkUpgrade(false,true)


        //测试升级
//        val data = UpdateBean(2,"1.0.1","发现新版本","1、修复了已知可能影响体验的BUG\n" +
//                "                \\n2、提升直播及在线聊天的稳定性\\n3、多种优化等你发现","https://app.huacunkj.com/profile/upload/2022/10/09/xiaomi_1.0.1.apk",0,0)  //这是我这里后台返回的Model，你可以根据自己项目进行修改
//        showUpgradeDialog(data)
        //请求位置--连续定位
        lifecycleScope.launchWhenCreated {
            CurrentLocationUtils.getStartLocal(this@MainActivity, object : ChoiceLocation {
                override fun onRestuse(lat: String, lng: String, address: String) {
                    //todo:
                   vm.toUpdateLocation(lat,lng)
                }
            })
        }

    }

    override fun listeners() {
        super.listeners()

        binding.llOne.onClick {
            binding.imgOne.setImageResource(R.drawable.menu_one_focus)
            binding.tvOne.setTextColor(Color.parseColor("#333333"))
            binding.imgTwo.setImageResource(R.drawable.menu_two)
            binding.tvTwo.setTextColor(Color.parseColor("#999999"))
            binding.imgThree.setImageResource(R.drawable.menu_three)
            binding.tvThree.setTextColor(Color.parseColor("#999999"))
            switchContent(0)

        }
        binding.llTwo.onClick {
            binding.imgOne.setImageResource(R.drawable.menu_one)
            binding.tvOne.setTextColor(Color.parseColor("#999999"))
            binding.imgTwo.setImageResource(R.drawable.men_two_focus)
            binding.tvTwo.setTextColor(Color.parseColor("#333333"))
            binding.imgThree.setImageResource(R.drawable.menu_three)
            binding.tvThree.setTextColor(Color.parseColor("#999999"))
            switchContent(1)
        }
        binding.llThree.onClick {
            binding.imgOne.setImageResource(R.drawable.menu_one)
            binding.tvOne.setTextColor(Color.parseColor("#999999"))
            binding.imgTwo.setImageResource(R.drawable.menu_two)
            binding.tvTwo.setTextColor(Color.parseColor("#999999"))
            binding.imgThree.setImageResource(R.drawable.menu_three_focus)
            binding.tvThree.setTextColor(Color.parseColor("#333333"))
            switchContent(2)
        }
    }




    private fun initData() {
        initMainMenu()

    }

    private fun initMainMenu() {
        val oneFragment = HomeFragment()
        val twoFragment = HomeOrderFragment()
        val threeFragment = HomeMeFragemnt()


        fragments = arrayOf(
            oneFragment,
            twoFragment,
            threeFragment

        )

        switchContent(0)

    }


    private fun switchContent(index: Int) {

        val childFragmentManager: FragmentManager = supportFragmentManager
        val transaction = childFragmentManager.beginTransaction()
        var fragment = childFragmentManager.findFragmentByTag("tab_$index")
        if (fragment == null) {//还未加载

            fragment = fragments[index]
            if (fragment != null) {
                transaction.add(R.id.container, fragment, "tab_$index")
            }
        } else {
            transaction.show(fragment)
        }
        for (i in fragments.indices) {
            fragment = childFragmentManager.findFragmentByTag("tab_$i")
            if (fragment == null || index == i) {
                continue
            }
            transaction.hide(fragment)
        }
        transaction.commitAllowingStateLoss()
    }


    override fun vmClass(): Class<MainVModel> {
        return MainVModel::class.java
    }

    override fun binding(): ActivityMainBinding {
        return ActivityMainBinding.inflate(layoutInflater)
    }

    override fun title(): String? {
        return null
    }

    override fun transluent(): Boolean {
        return true
    }

    /**
     * 双击退出函数
     */
    private var isExit = false

    /**
     * 菜单、返回键响应
     */
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitBy2Click() //调用双击退出函数
            return true
        }
        return false
    }

    private fun exitBy2Click() {
        var tExit: Timer? = null
        if (!isExit) {
            isExit = true // 准备退出
            toast("再按一次退出应用")
            tExit = Timer()
            tExit.schedule(object : TimerTask() {
                override fun run() {
                    isExit = false // 取消退出
                }
            }, 1500) // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务
        } else {
            KeySimulateUtils.simulate(KeyEvent.KEYCODE_HOME)
            System.exit(0)
        }
    }


}

